package com.mercury.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.mercury.article.entity.HomeArticle;
import com.mercury.article.entity.HomeArticleDetail;
import com.mercury.article.entity.vo.ArticleInfoVo;
import com.mercury.article.mapper.HomeArticleMapper;
import com.mercury.article.service.HomeArticleDetailService;
import com.mercury.article.service.HomeArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mercury.servicebase.exceptionhandler.GuliException;
import com.mercury.util.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import javax.swing.undo.CannotUndoException;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zwn
 * @since 2021-10-26
 */
@Service
public class HomeArticleServiceImpl extends ServiceImpl<HomeArticleMapper, HomeArticle> implements HomeArticleService {

    @Autowired
    private HomeArticleDetailService detailService;

    /**
     * @date 2021/10/28 8:36
     * @params centerUser
     * @return  CenterUser
     * @description 生成文章的uuid
     */
    public String encryptionAndUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * @date 2021/11/4 10:25
     * @params
     * @return  ArrayList<HomeArticle>
     * @description 返回文章列表
    */
    @Override
    public ArrayList<HomeArticle> showArticleList() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("deleted", 0);
        ArrayList<HomeArticle> homeArticleList = (ArrayList<HomeArticle>) this.listByMap(map);
        if (homeArticleList.size() == 0){
            return null;
        }else {
            return homeArticleList;
        }
    }

    /**
     * @date 2021/11/4 10:16
     * @params articleInfoVo
     * @return 文章 id
     * @description 向文章分类列表和文章详情列表添加
    */
    @Override
    public String addArticle(ArticleInfoVo articleInfoVo) {
        //向文章列表添加记录
        HomeArticle homeArticle = new HomeArticle();
        BeanUtils.copyProperties(articleInfoVo,homeArticle);
        String id = encryptionAndUUID();
        homeArticle.setArticleId(id);
        homeArticle.setDeleted(false);
        int insert = baseMapper.insert(homeArticle);
        if(insert < 1){
            throw new GuliException(20001,"添加文章失败");
        }

        //向文章详情表中添加数据
        HomeArticleDetail articleDetail = new HomeArticleDetail();
        BeanUtils.copyProperties(articleInfoVo,articleDetail);
        articleDetail.setArticleId(id);
        articleDetail.setDeleted(false);
        boolean save = detailService.save(articleDetail);
        if(!save) throw new GuliException(20001,"添加文章详情失败");

        return id;
    }

    /**
     * @date 2021/11/7 15:41
     * @params articleInfoVo
     * @return int 改变的数据条数
     * @description TODO 更新文章
    */
    @Override
    public int updateArticle(ArticleInfoVo articleInfoVo) {
        //向文章列表添加记录
//        HomeArticle homeArticle = new HomeArticle();
//        BeanUtils.copyProperties(articleInfoVo,homeArticle);
//        homeArticle.setDeleted(false);
//        baseMapper.updateById(homeArticle);
//        int insert = baseMapper.insert(homeArticle);
//        if(insert < 1){
//            throw new GuliException(20001,"添加文章失败");
//        }
//
//        //向文章详情表中添加数据
//        HomeArticleDetail articleDetail = new HomeArticleDetail();
//        BeanUtils.copyProperties(articleInfoVo,articleDetail);
//        articleDetail.setArticleId(id);
//        articleDetail.setDeleted(false);
//        boolean save = detailService.save(articleDetail);
//        if(!save) throw new GuliException(20001,"添加文章详情失败");

        return 0;
    }

    @Override
    public ArticleInfoVo getArticleById(String id) {
        HomeArticle homeArticle = baseMapper.selectById(id);
        ArticleInfoVo articleInfoVo = new ArticleInfoVo();
        BeanUtils.copyProperties(homeArticle, articleInfoVo);

        HomeArticleDetail articleDetail = detailService.getDetailById(id);
        BeanUtils.copyProperties(articleDetail, articleInfoVo);

        return articleInfoVo;
    }

    @Override
    public List<HomeArticle> getArticleByType(int type) {
        QueryWrapper<HomeArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("article_type",type);
        List<HomeArticle> articleList = baseMapper.selectList(wrapper);
        return articleList;
    }
}
